package com.lzh.controller;

import com.github.pagehelper.PageInfo;
import com.lzh.pojo.CT;
import com.lzh.pojo.Class;
import com.lzh.pojo.Teacher;
import com.lzh.service.CTService;
import com.lzh.service.TeacherService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.Format;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.SimpleFormatter;

/**
 * @author reborn
 * @create 2020/5/17-23:54
 */

@Controller
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @Autowired
    CTService ctService;

    @RequestMapping("/")
    public String sy(HttpSession session) {
        if (session.getAttribute("teaAll")!=null){
            return "dashboard";
        }
        Teacher teacher= new Teacher(100, "未登录");
        session.setAttribute("teaAll",teacher);
        return "dashboard";
    }

    @RequestMapping("/t2")
    public String da() {
        return "dashboard";
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/toRegist")
    public String toRegiste() {
        return "regist";
    }

    @RequestMapping("/t3")
    public String test3() {
        return "teacher/teacher";
    }




    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/";
    }


    //查看所有老师，及他们的信息
    @RequestMapping("/selCoAll")
    public String selALlTea(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                            Model model) {
        PageInfo<Teacher> teachers = teacherService.selectTeachers(pageNum, pageSize);
        System.out.println(teachers);
        model.addAttribute("pageInfo", teachers);
        model.addAttribute("type", "all");
        return "teacher/colleague";
    }


    //根据名字查询
    @RequestMapping("/selectTeaByName")
    public String selectTeaByName(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                  String name, Model model) {
        PageInfo<Teacher> teacherPageInfo = teacherService.selectTeaByName(pageNum, pageSize, name);
        model.addAttribute("pageInfo", teacherPageInfo);
        model.addAttribute("type", "name");
        model.addAttribute("name1", name);
        return "teacher/colleague";
    }

    //查看同班的老师，及他们的信息
    @RequestMapping("/selTogeTea")
    public String selTogeTea(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                             int id, Model model) {
        List<Integer> integers = ctService.selectCidByTid(id);
        for (Integer integer : integers) {
            if (integer.equals(100)) {
                System.out.println(integer);
                integers.remove(integer);
                System.out.println(integer);
                break;
            }
        }
        integers.forEach(System.out::println);
        Set<Integer> tids = ctService.selectTidByCid(integers);
        PageInfo<Teacher> teachers = teacherService.selectTeacherByIds(pageNum, pageSize, tids);
        System.out.println(teachers);
        model.addAttribute("tids", tids);
        model.addAttribute("pageInfo", teachers);
        model.addAttribute("type", "toge");
        return "teacher/colleague";
    }


    //查看权限的老师，及他们的信息
    @RequestMapping("/selectPerms")
    public String selectPerms(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                              String perms, Model model) {
        PageInfo<Teacher> teacherPageInfo = teacherService.selectPerms(pageNum, pageSize, perms);
        model.addAttribute("pageInfo", teacherPageInfo);
        model.addAttribute("perms", perms);
        model.addAttribute("type", "perms");
        return "teacher/colleague";
    }

    //前往管理员页面
    @RequestMapping("/t6")
    public String test6() {
        return "teacher/data";
    }

    //前往更新页面
    @RequestMapping("/toUpdTea")
    public String updateTea(int id, Model model) {
        Teacher teacher = teacherService.selectTeacherById(id);
        model.addAttribute("teacher", teacher);
        System.out.println(teacher);
        model.addAttribute("type", "mes");
        return "teacher/update";
    }

    //前往更新页面，仅修改密码
    @RequestMapping("/toUpdTeaPwd")
    public String updateTeaPwd(int id, Model model) {
        Teacher teacher = teacherService.selectTeacherById(id);
        model.addAttribute("teacher", teacher);
        System.out.println(teacher);
        model.addAttribute("type", "pwd");
        return "teacher/update";
    }

////    登录验证
//    @PostMapping("/login")
//    public String login(String zh, String pwd, Model model, HttpSession session){
//        Teacher teacher = teacherService.selectTeacherZh(zh, pwd);
//        if (teacher==null){
//            model.addAttribute("msg","帐号不存在或者密码有误");
//            return "index";
//        }else {
//            teacher.setLoginTime(new Date());
//            teacherService.updateTeacher(teacher);
//            session.setAttribute("teaAll",teacher);
//            session.setAttribute("tid",teacher.getId());
//            return "/dashboard";
//        }
//    }

    //    登录验证
    @PostMapping("/login")
    public String login(String zh, String pwd,boolean rememberMe, Model model, HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        Teacher teacher = teacherService.selectTeacherZh(zh, pwd);
        List<Integer> integers = ctService.selectCidByTid(teacher.getId());
        UsernamePasswordToken token = new UsernamePasswordToken(zh, pwd,rememberMe);
        try {
            System.out.println(teacher);
            subject.login(token);
            teacher.setLoginTime(new Date());
            teacherService.updateTeacher(teacher);
            session.setAttribute("teaAll", teacher);
            session.setAttribute("cids", integers);
            session.setAttribute("tid", teacher.getId());
            return "dashboard";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "帐号不存在");
            return "index";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码有误");
            return "index";

        }
    }

    //    修改基本信息
    @RequestMapping("/updTea")
    public String UpdTea(Teacher teacher, Model model) {

        Teacher teacher1 = teacherService.selectZh(teacher.getZh());
        if (teacher1 != null) {
            model.addAttribute("error", "帐号存在，请换一个");
            model.addAttribute("teacher", teacher);
            return "teacher/update";
        } else {
            teacherService.updateTeacher(teacher);
            System.out.println(teacher);
            return "redirect:/teaMe";
        }

    }


    //    修改密码
    @RequestMapping("/updTeaPwd")
    public String UpdTeaPwd(Teacher teacher, String pwdOld, Model model) {
        if (pwdOld != teacher.getPwd()) {
            model.addAttribute("type", "pwd");
            model.addAttribute("error", "原密码有误");
            model.addAttribute("teacher", teacher);
            return "teacher/update";
        }
        teacherService.updateTeacher(teacher);
        return "redirect:/teaMe";
    }

    //    显示教师个人信息
    @RequestMapping("/teaMe")
    public String TeaMessage(int id, Model model, HttpSession session) {
        Teacher teacher = teacherService.selectTeacherById(id);
        List<Integer> cts = ctService.selectCidByTid(id);
        List<Class> classes = ctService.selectCname(cts);
        System.out.println(cts);
        System.out.println(classes);
        model.addAttribute("cname", classes);
        model.addAttribute("teacher", teacher);
        System.out.println(teacher);
        session.setAttribute("tid", id);
        System.out.println(id);
        return "teacher/teacher";
    }

    @RequestMapping("/registTea")
    public String registTea(Teacher teacher, Model model) {
        Teacher teacher1 = teacherService.selectZh(teacher.getZh());
        if (teacher1 != null) {
            model.addAttribute("error", "帐号已存在");
            return "regist";
        }
        model.addAttribute("teacher", teacher);
        teacher.setRegistTime(new Date());
        teacherService.insertTea(teacher);
        Teacher teacher2 = teacherService.selectZh(teacher.getZh());
        System.out.println(teacher2.getId());
        CT ct = new CT(100, teacher2.getId());
        ctService.addTtoC(ct);
        System.out.println(teacher);
        return "success";
    }



}
